﻿using GalaSoft.MvvmLight;
using HighThroughput.Common;
using Newtonsoft.Json;
using OSSUtility;
using Ozone.Device;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Ozone.Models
{

    public class DevReadData
    {
        public DevReadData()
        {

        }
        public RD_S10 rD_S10 { get; set; } = new RD_S10();
    }

    //加氢仪 S10
    public class RD_S10:ObservableObject
    {

        public void RaisePropertyChangedPro()
        {
            RaisePropertyChanged(String.Empty);
        }

        //是否为采集过来的数据
        public bool bIsCollect { set; get; } = false;

        //是否连接
        public bool bConnected { set; get; }


        //泵开关
        public string PumpSwitch1 { set; get; } = "";
        public string PumpSwitch2 { set; get; }
        //氢气流量计保护阀开关
        public string HMFCValveSwitch { set; get; }
        //氮气流量计保护阀开关
        public string NMFCValveSwitch { set; get; }
        //气液分离器开关
        public string GasLiquidSeparatorSwitch { set; get; }
        //稳压阀开关
        public string VentingValveSwitch { set; get; }


        //泵流速
        public string PumpFlow1 { set; get; } = "";
        public string PumpFlowUnit1
        {
            get { return Tools.RoundPro(PumpFlow1,3) + " mL/min"; }

        }
        public string PumpFlow2 { set; get; } = "";
        public string PumpFlowUnit2
        {
            get { return Tools.RoundPro(PumpFlow2, 3) + " mL/min"; }

        }

        //泵压力
        public string PumpPress1 { set; get; } = "";

        public string PumpPressUnit1
        {
            get { return  Tools.RoundPro(PumpPress1) + " MPa"; }

        }
        public string PumpPress2 { set; get; } = "";

        public string PumpPressUnit2
        {
            get { return Tools.RoundPro(PumpPress2) + " MPa"; }

        }

        //流量计1
        public string MFCSwitch1 { set; get; }
        public string MFCPress1 { set; get; }
        public string MFCPress1Unit
        {
            get { return Tools.RoundPro(MFCPress1) + " MPa"; }
        }
        public string MFCFlow1 { set; get; } 

        public string MFCFlow1Unit
        {
            get { return Tools.RoundPro(MFCFlow1) + " mL/min"; }
        }

        //流量计2
        public string MFCSwitch2 { set; get; }
        public string MFCPress2 { set; get; }
        public string MFCPress2Unit
        {
            get { return Tools.RoundPro(MFCPress2) + " MPa"; }
        }

        public string MFCFlow2 { set; get; }

        public string MFCFlow2Unit
        {
            get { return Tools.RoundPro(MFCFlow2) + " mL/min"; }
        }

        //流量计3
        public string MFCSwitch3 { set; get; }
        public string MFCPress3 { set; get; }
        public string MFCPress3Unit
        {
            get { return Tools.RoundPro(MFCPress3) + " MPa"; }
        }
        public string MFCFlow3 { set; get; }

        public string MFCFlow3Unit
        {
            get { return Tools.RoundPro(MFCFlow3) + " mL/min"; }
        }

        //流量计4
        public string MFCSwitch4 { set; get; }
        public string MFCPress4 { set; get; }
        public string MFCPress4Unit
        {
            get { return Tools.RoundPro(MFCPress4) + " MPa"; }
        }
        public string MFCFlow4 { set; get; }

        public string MFCFlow4Unit
        {
            get { return Tools.RoundPro(MFCFlow4) + " mL/min"; }
        }

        //流量计5
        public string MFCSwitch5 { set; get; }
        public string MFCPress5 { set; get; }
        public string MFCPress5Unit
        {
            get { return Tools.RoundPro(MFCPress5) + " MPa"; }
        }
        public string MFCFlow5 { set; get; }

        public string MFCFlow5Unit
        {
            get { return Tools.RoundPro(MFCFlow5) + " mL/min"; }
        }

        //流量计6
        public string MFCSwitch6 { set; get; } = "";
        public string MFCPress6 { set; get; } = "";
        public string MFCPress6Unit
        {
            get { return Tools.RoundPro(MFCPress6) + " MPa"; }
        }
        public string MFCFlow6 { set; get; } = "";

        public string MFCFlow6Unit
        {
            get { return Tools.RoundPro(MFCFlow6) + " mL/min"; }
        }

        //流量计7
        public string MFCSwitch7 { set; get; } = "";
        public string MFCPress7 { set; get; } = "";
        public string MFCPress7Unit
        {
            get { return Tools.RoundPro(MFCPress7) + " MPa"; }
        }
        public string MFCFlow7 { set; get; } = "";

        public string MFCFlow7Unit
        {
            get { return Tools.RoundPro(MFCFlow7) + " mL/min"; }
        }

        //流量计8
        public string MFCSwitch8 { set; get; } = "";
        public string MFCPress8 { set; get; } = "";
        public string MFCPress8Unit
        {
            get { return Tools.RoundPro(MFCPress8) + " MPa"; }
        }
        public string MFCFlow8 { set; get; } = "";

        public string MFCFlow8Unit
        {
            get { return Tools.RoundPro(MFCFlow8) + " mL/min"; }
        }

        //预热器温度1
        public string PreheaterTemp1 { set; get; } = "";

        public string PreheaterTemp1Unit
        {
            get { return Tools.RoundPro(PreheaterTemp1,1) + " ℃"; }
        }

        //预热器温度2
        public string PreheaterTemp2 { set; get; } = "";

        public string PreheaterTemp2Unit
        {
            get { return Tools.RoundPro(PreheaterTemp2,1) + " ℃"; }
        }

        //预热器温度3
        public string PreheaterTemp3 { set; get; } = "";

        public string PreheaterTemp3Unit
        {
            get { return Tools.RoundPro(PreheaterTemp3,1) + " ℃"; }
        }

        //柱管温度温度1
        public string ColumnTemp1 { set; get; } = "";

        public string ColumnTemp1Unit
        {
            get { return Tools.RoundPro(ColumnTemp1,1) + " ℃"; }
        }

        //柱管温度温度2
        public string ColumnTemp2 { set; get; } = "";

        public string ColumnTemp2Unit
        {
            get { return Tools.RoundPro(ColumnTemp2,1) + " ℃"; }
        }


        //背压阀压力1
        public string BPPress1 { set; get; } = "";
        public string BPPress1Unit
        {
            get { return Tools.RoundPro(BPPress1,1) + " MPa"; }
        }

        //背压阀压力2
        public string BPPress2 { set; get; } = "";
        public string BPPress2Unit
        {
            get { return Tools.RoundPro(BPPress2,1) + " MPa"; }
        }

        //气液分离器开关
        public string GasLiquidSeparatorSwitch1 { set; get; } = "0";
        public string GasLiquidSeparatorSwitch2 { set; get; } = "0";

        //稳流阀开关
        public string VentingValveSwitch1 { set; get; } = "0";
        public string VentingValveSwitch2 { set; get; } = "0";

        //四通阀状态
        public string FourValveSwitch { set; get; } = "1";

        //保温箱
        public string IncubatorSwitch { set; get; } = "";
        public string IncubatorTemp { set; get; }
        public string IncubatorTempUnit
        {
            get { return Tools.RoundPro(IncubatorTemp,1) + " ℃"; }
        }

        //加热带
        public string HeatingBandTemp { set; get; }
        public string HeatingBandTempUnit
        {
            get { return Tools.RoundPro(HeatingBandTemp) + " ℃"; }
        }

        //液位
        public string LiquidLevel1 { set; get; }
        public string LiquidLevel2 { set; get; }

        public string LiquidLevelUnit1
        {
            get { return Tools.RoundPro(LiquidLevel1, 0) + "%"; }
        }

        public string LiquidLevelUnit2
        {
            get { return Tools.RoundPro(LiquidLevel2, 0) + "%"; }
        }

        //吹扫状态
        public string PurgeSwitch { set; get; } = "";
        public string PurgeSwitchUnit
        {
            get { if (PurgeSwitch == "1") return "开"; else return "关"; }
        }
        public bool PurgeSwitchStatus0
        {
            get { if (PurgeSwitch == "1") return true ; else return false; }
        }

        public bool PurgeSwitchStatus1
        {
            get { if (PurgeSwitch == "1") return false; else return true; }
        }



        //背压阀切换气液分离器状态
        public string SwitchLCBP1 { get; set; } = "0";
        public string SwitchLCBP2 { get; set; } = "0";
        public RD_S10()
        {
            bConnected = false;
            bIsCollect = false;
           
        }

        public string getValByID(string FDataID)
        {
            switch (FDataID)
            {
                //
                case "PumpSwitch1":
                    return PumpSwitch1;
                case "PumpSwitch2":
                    return PumpSwitch2;

                case "PumpFlow1":
                    return PumpFlow1;
                case "PumpPress1":
                    return PumpPress1;

                case "MFCFlow1":
                    return MFCFlow1;
                case "MFCFlow2":
                    return MFCFlow2;
                case "MFCFlow3":
                    return MFCFlow3;
                case "MFCFlow4":
                    return MFCFlow4;
                case "MFCFlow5":
                    return MFCFlow5;
                case "MFCFlow6":
                    return MFCFlow6;
                case "MFCFlow7":
                    return MFCFlow7;
                case "MFCFlow8":
                    return MFCFlow8;

                case "MFCSwitch1":
                    return MFCSwitch1;
                case "MFCSwitch2":
                    return MFCSwitch2;
                case "MFCSwitch3":
                    return MFCSwitch3;
                case "MFCSwitch4":
                    return MFCSwitch4;
                case "MFCSwitch5":
                    return MFCSwitch5;
                case "MFCSwitch6":
                    return MFCSwitch6;
                case "MFCSwitch7":
                    return MFCSwitch7;
                case "MFCSwitch8":
                    return MFCSwitch8;

                case "PreheaterTemp1":
                    return PreheaterTemp1;
                case "PreheaterTemp2":
                    return PreheaterTemp2;
                case "PreheaterTemp3":
                    return PreheaterTemp3;

                case "ColumnTemp1":
                    return ColumnTemp1;
                case "ColumnTemp2":
                    return ColumnTemp2;

                case "BPPress1":
                    return BPPress1;
                case "BPPress2":
                    return BPPress2;

                case "FourValveSwitch":
                    return FourValveSwitch;

                case "IncubatorTemp":
                    return IncubatorTemp;
                case "IncubatorSwitch":
                    return IncubatorSwitch;

                case "HeatingBandTemp":
                    return HeatingBandTemp;

                case "PurgeSwitch":
                    return PurgeSwitch;

                case "LiquidLevel1":
                    return LiquidLevel1;

                case "LiquidLevel2":
                    return LiquidLevel2;



    }

            return "";
        }


        public string getValByID1(string FDataID)
        {
            object obj = this;
            Type t = obj.GetType();//获取对象类型


            foreach (PropertyInfo fi in t.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
            {
                if (FDataID == fi.Name)
                {
                    
                    object vv= fi.GetValue(t);
                    if (vv == null)
                        return "";
                    else
                        return vv.ToString();
                }
            }
            return "";
        }

      
        public void setValues(RD_S10 rd)
        {
            MFCFlow1 = rd.MFCFlow1;
            MFCFlow2 = rd.MFCFlow2;
            MFCFlow3 = rd.MFCFlow3;
            MFCFlow4 = rd.MFCFlow4;
            MFCFlow5 = rd.MFCFlow5;
            MFCFlow6 = rd.MFCFlow6;
            MFCFlow7 = rd.MFCFlow7;
            MFCFlow8 = rd.MFCFlow8;

            MFCPress1 = rd.MFCPress1;
            MFCPress2 = rd.MFCPress2;
            MFCPress3 = rd.MFCPress3;
            MFCPress4 = rd.MFCPress4;
            MFCPress5 = rd.MFCPress5;
            MFCPress6 = rd.MFCPress6;
            MFCPress7 = rd.MFCPress7;
            MFCPress8 = rd.MFCPress8;

            MFCSwitch1 = rd.MFCSwitch1;
            MFCSwitch2 = rd.MFCSwitch2;
            MFCSwitch3 = rd.MFCSwitch3;
            MFCSwitch4 = rd.MFCSwitch4;
            MFCSwitch5 = rd.MFCSwitch5;
            MFCSwitch6 = rd.MFCSwitch6;
            MFCSwitch7 = rd.MFCSwitch7;
            MFCSwitch8 = rd.MFCSwitch8;

            PumpSwitch1 = rd.PumpSwitch1;
            PumpFlow1 = rd.PumpFlow1;
            PumpPress1 = rd.PumpPress1;

            PumpSwitch2 = rd.PumpSwitch2;
            PumpFlow2 = rd.PumpFlow2;
            PumpPress2 = rd.PumpPress2;

            ColumnTemp1 = rd.ColumnTemp1;
            ColumnTemp2 = rd.ColumnTemp2;

            PreheaterTemp1 = rd.PreheaterTemp1;
            PreheaterTemp2 = rd.PreheaterTemp2;

            BPPress1 = rd.BPPress1;
            BPPress2 = rd.BPPress2;

            IncubatorTemp = rd.IncubatorTemp;
            HeatingBandTemp = rd.HeatingBandTemp;

            FourValveSwitch =  rd.FourValveSwitch;

            GasLiquidSeparatorSwitch1 = rd.GasLiquidSeparatorSwitch1;
            GasLiquidSeparatorSwitch2 = rd.GasLiquidSeparatorSwitch2;

            LiquidLevel1 = (Utility.str2int( rd.LiquidLevel1) / 10).ToString();
            LiquidLevel2 = (Utility.str2int(rd.LiquidLevel2) / 10).ToString();

            VentingValveSwitch1 = rd.GasLiquidSeparatorSwitch1;
            VentingValveSwitch2 = rd.GasLiquidSeparatorSwitch2;



        }
    }


    public class DevConfigData
    {
        public string FDataID { set; get; } = "";
        public string Title { set; get; } = "";
        public string FRang { set; get; } = "";

        public string FUnit { set; get; } = "";

        //区域 0：1区 1:2区
        public int FArea { set; get; } = -1;

        //条件标志
        public bool ConditionFlag { set; get; } = true;

        //动作标志
        public bool ActFlag { set; get; } = true;

      
    }

    public class DevConfigMain
    {
        static public DevConfigMain Entity { set; get; } = new DevConfigMain();

        public List<DevConfigData> lstDevConfig { set; get; }

        public DevConfigMain()
        {
            lstDevConfig = new List<DevConfigData>();

        
            //1 区
            
            lstDevConfig.Add(new DevConfigData() { FDataID = "MFCFlow1", Title = "A气体流量1", FRang = "0-50", FUnit = "(mL/min)", FArea = 0 });
            lstDevConfig.Add(new DevConfigData() { FDataID = "MFCFlow2", Title = "A气体流量2", FRang = "0-50", FUnit = "(mL/min)", FArea = 0 });
            lstDevConfig.Add(new DevConfigData() { FDataID = "MFCFlow3", Title = "A气体流量3", FRang = "0-100", FUnit = "(mL/min)", FArea = 0 });
            lstDevConfig.Add(new DevConfigData() { FDataID = "MFCFlow4", Title = "A气体流量4", FRang = "0-50", FUnit = "(mL/min)", FArea = 1 });
           
            lstDevConfig.Add(new DevConfigData() { FDataID = "MFCFlow5", Title = "B气体流量5", FRang = "0-50", FUnit = "(mL/min)", FArea = 1 });
            lstDevConfig.Add(new DevConfigData() { FDataID = "MFCFlow6", Title = "B气体流量6", FRang = "0-100", FUnit = "(mL/min)", FArea = 1 });
            lstDevConfig.Add(new DevConfigData() { FDataID = "MFCFlow7", Title = "B气体流量7", FRang = "0-50", FUnit = "(mL/min)", FArea = 1 });
            lstDevConfig.Add(new DevConfigData() { FDataID = "MFCFlow8", Title = "B气体流量8", FRang = "0-100", FUnit = "(mL/min)", FArea = 1 });

            lstDevConfig.Add(new DevConfigData() { FDataID = "MFCSwitch1", Title = "A气体流量1开关", FRang = "0-50", FUnit = "(mL/min)", FArea = 0 });
            lstDevConfig.Add(new DevConfigData() { FDataID = "MFCSwitch2", Title = "A气体流量2开关", FRang = "0-50", FUnit = "(mL/min)", FArea = 0 });
            lstDevConfig.Add(new DevConfigData() { FDataID = "MFCSwitch3", Title = "A气体流量3开关", FRang = "0-100", FUnit = "(mL/min)", FArea = 0 });
            lstDevConfig.Add(new DevConfigData() { FDataID = "MFCSwitch4", Title = "A气体流量4开关", FRang = "0-50", FUnit = "(mL/min)", FArea = 1 });

            lstDevConfig.Add(new DevConfigData() { FDataID = "MFCSwitch5", Title = "B气体流量1开关", FRang = "0-50", FUnit = "(mL/min)", FArea = 1 });
            lstDevConfig.Add(new DevConfigData() { FDataID = "MFCSwitch6", Title = "B气体流量2开关", FRang = "0-100", FUnit = "(mL/min)", FArea = 1 });
            lstDevConfig.Add(new DevConfigData() { FDataID = "MFCSwitch7", Title = "B气体流量3开关", FRang = "0-50", FUnit = "(mL/min)", FArea = 1 });
            lstDevConfig.Add(new DevConfigData() { FDataID = "MFCSwitch8", Title = "B气体流量4开关", FRang = "0-100", FUnit = "(mL/min)", FArea = 1 });


            lstDevConfig.Add(new DevConfigData() { FDataID = "PreheaterTemp1", Title = "A预热器温度", FRang = "1-250", FUnit = "(℃)", FArea = 0 });
            lstDevConfig.Add(new DevConfigData() { FDataID = "PreheaterTemp2", Title = "B预热器温度", FRang = "1-250", FUnit = "(℃)", FArea = 0 });
            lstDevConfig.Add(new DevConfigData() { FDataID = "ColumnTemp1", Title = "A反应柱温度", FRang = "1-500", FUnit = "(℃)", FArea = 0 });
            lstDevConfig.Add(new DevConfigData() { FDataID = "BPPress1", Title = "A背压阀压力", FRang = "0-10", FUnit = "(MPa)", ActFlag = true, FArea = 0 });
            lstDevConfig.Add(new DevConfigData() { FDataID = "BPPress2", Title = "B背压阀压力", FRang = "0-10", FUnit = "(MPa)", ActFlag = true, FArea = 1 });

            //2 区
            lstDevConfig.Add(new DevConfigData() { FDataID = "PreheaterTemp3", Title = "2-气体预热器温度", FRang = "1-250", FUnit = "(℃)", FArea = 1 });
            lstDevConfig.Add(new DevConfigData() { FDataID = "ColumnTemp2", Title = "B反应柱温度", FRang = "1-500", FUnit = "(℃)", FArea = 1 });

            //公共区
            lstDevConfig.Add(new DevConfigData() { FDataID = "PumpSwitch1", Title = "A泵开关", FRang = "" });
            lstDevConfig.Add(new DevConfigData() { FDataID = "PumpPress1", Title = "A泵压力", FRang = "", ActFlag = false }); ;
            lstDevConfig.Add(new DevConfigData() { FDataID = "PumpFlow1", Title = "A泵流速", FRang = "0.001-3", FUnit = "(mL/min)" });
            lstDevConfig.Add(new DevConfigData() { FDataID = "PumpSwitch2", Title = "B泵开关", FRang = "" });
            lstDevConfig.Add(new DevConfigData() { FDataID = "PumpPress2", Title = "B泵压力", FRang = "", ActFlag = false }); ;
            lstDevConfig.Add(new DevConfigData() { FDataID = "PumpFlow2", Title = "B泵流速", FRang = "0.001-3", FUnit = "(mL/min)" });


            lstDevConfig.Add(new DevConfigData() { FDataID = "IncubatorTemp", Title = "保温箱温度", FRang = "1-250", FUnit = "(℃)" });
            lstDevConfig.Add(new DevConfigData() { FDataID = "IncubatorSwitch", Title = "保温箱开关", FRang = "", ActFlag = false, ConditionFlag = false });
            lstDevConfig.Add(new DevConfigData() { FDataID = "HeatingBandTemp", Title = "加热带温度", FRang = "1-250", FUnit = "(℃)" });

            lstDevConfig.Add(new DevConfigData() { FDataID = "FourValveSwitch", Title = "四通阀开关", FRang = "" });
            lstDevConfig.Add(new DevConfigData() { FDataID = "PurgeSwitch", Title = "吹扫开关", FRang = "" });
        }

        public string getTitle(string dataID)
        {
            for(int i=0;i<lstDevConfig.Count;i++)
            {
                if (dataID == lstDevConfig[i].FDataID)
                    return lstDevConfig[i].Title;
            }

            return "";
        }


        public string getRang(string dataID)
        {
            for (int i = 0; i < lstDevConfig.Count; i++)
            {
                if (dataID == lstDevConfig[i].FDataID)
                    return lstDevConfig[i].FRang;
            }

            return "";
        }

        public string IsInRang(string dataID,double v)
        {
            string str = "";
            DevConfigData dev = null;
            for (int i=0;i<lstDevConfig.Count;i++)
            {
                if(dataID == lstDevConfig[i].FDataID)
                {
                    dev = lstDevConfig[i];
                    break;
                }
            }

            if(dev == null)
            {
                str =  "设备不存在";
            }

            string[] arRang = dev.FRang.Split('-');
            if (arRang.Length != 2)
                return  "";



            if (!Tools.IsInRang((v),Utility.str2double(arRang[0]),Utility.str2double(arRang[1])))
            {
                str = dev.Title + "超出范围,范围" + dev.FRang + dev.FUnit;
            }

            return str;
        }
    }


}
